/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.phoenix.repository.iterator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.jcr.Property;
import javax.jcr.PropertyIterator;

/**
 * Jigsaw server
 * 
 * @author <a href="shamphone@gmail.com">Li XiongFeng</a>
 * @date 2012-1-3
 * @version 1.0.0
 */
public class BasicPropertyIterator implements PropertyIterator {
	private List<Property> collection;
	private int position;
	private int size;

	public BasicPropertyIterator(Collection<Property> collection) {
		this.collection = new ArrayList<Property>();
		this.collection.addAll(collection);
		this.size = this.collection.size();
		this.position = -1;
	}
	
	public void addAll(Collection<Property> propertyList){
		this.collection.addAll(propertyList);
		this.size = this.collection.size();
		this.position = -1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.RangeIterator#skip(long)
	 */
	@Override
	public void skip(long skipNum) {
		this.position += skipNum;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.RangeIterator#getSize()
	 */
	@Override
	public long getSize() {
		return this.size;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.RangeIterator#getPosition()
	 */
	@Override
	public long getPosition() {
		return this.position;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.Iterator#hasNext()
	 */
	@Override
	public boolean hasNext() {
		return this.position < this.size - 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.Iterator#next()
	 */
	@Override
	public Object next() {
		this.position++;
		return this.collection.get(position);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.Iterator#remove()
	 */
	@Override
	public void remove() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.PropertyIterator#nextProperty()
	 */
	@Override
	public Property nextProperty() {
		return (Property) this.next();
	}

}
